package com.cee.admin.modules.pm.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cee.common.web.result.Result;
import com.cee.domain.modules.pm.entity.PmCarbonMarketInfo;
import com.cee.domain.modules.pm.vo.PmCarbonMarketInfoVo;
import com.cee.domain.modules.sys.entity.User;
import com.cee.ds.modules.pm.service.IPmCarbonMarketInfoService;
import com.cee.ds.modules.sys.service.IUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.cee.admin.common.BaseController;

import java.util.Date;

/**
 * <p>
 * 碳市场资讯表 前端控制器
 * </p>
 *
 * @author autoGenerator
 * @since 2024-04-18
 */
@Tag(name = "门户管理-碳市场资讯发布管理")
@RestController
@RequestMapping("/api/pm/carbon/market/info")
public class PmCarbonMarketInfoController extends BaseController {

    @Autowired
    private IPmCarbonMarketInfoService pmCarbonMarketInfoService;
    
    @Autowired
    private IUserService iUserService;


    @Operation(summary = "分页查询")
    @GetMapping("/page")
    public Result<Page<PmCarbonMarketInfoVo>> page(@RequestParam Long current, @RequestParam Long size, PmCarbonMarketInfoVo query){
        return success(pmCarbonMarketInfoService.queryPageVo(new Page<>(current, size),query));
    }

    @Operation(summary = "新增")
    @PostMapping("/save")
    @RequiresPermissions("pm:market:info:save")
    public Result<Boolean> save(@RequestBody PmCarbonMarketInfo marketInfo){
        marketInfo.setStatus("0");
        return success(pmCarbonMarketInfoService.saveOrUpdateMarketInfo(marketInfo));
    }

    @Operation(summary = "修改")
    @PostMapping("/update")
    @RequiresPermissions("pm:market:info:update")
    public Result<Boolean> update(@RequestBody PmCarbonMarketInfo marketInfo){
        return success(pmCarbonMarketInfoService.saveOrUpdateMarketInfo(marketInfo));
    }

    @Operation(summary = "发布")
    @PostMapping("/publish/{id}")
    @RequiresPermissions("pm:market:info:publish")
    public Result<Boolean> publish(@PathVariable("id") Long id){
        PmCarbonMarketInfo marketInfo = new PmCarbonMarketInfo();
        marketInfo.setId(id);
        marketInfo.setStatus("1");
        marketInfo.setPublishTime(new Date());
        return success(pmCarbonMarketInfoService.updateById(marketInfo));
    }

    @Operation(summary = "查看详情")
    @GetMapping("/get/{id}")
    @RequiresPermissions(value = {"pm:market:info:get","pm:market:info:update","pm:market:info:publish"})
    public Result<PmCarbonMarketInfo> get(@PathVariable("id") Long id){
        PmCarbonMarketInfo marketInfo = pmCarbonMarketInfoService.getById(id);
        if (marketInfo == null){
            return error("该碳市场资讯不存在");
        }
        User user = iUserService.getById(marketInfo.getCreateUserId());
        PmCarbonMarketInfoVo marketInfoVo = new PmCarbonMarketInfoVo();
        BeanUtils.copyProperties(marketInfo, marketInfoVo);
        marketInfoVo.setCreateUserName(user.getName());
        return success(marketInfoVo);
    }

    @Operation(summary = "删除")
    @DeleteMapping("/delete/{id}")
    @RequiresPermissions("pm:market:info:delete")
    public Result<Boolean> delete(@PathVariable("id") Long id){
        return success(pmCarbonMarketInfoService.removeMarketInfo(id));
    }


}
